package com.zhongwang.mms.module.allocation.controller;

import com.zhongwang.mms.base.BaseController;
import com.zhongwang.mms.common.JsonResult;
import com.zhongwang.mms.gen.entity.User;
import com.zhongwang.mms.module.allocation.model.AllocationOrderModel;
import com.zhongwang.mms.module.allocation.service.AllocationOrderService;
import com.zhongwang.mms.util.ShiroUtils;
import org.apache.ibatis.annotations.Param;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @description:调拨审核
 * @author: th
 * @time: 2020/9/3 8:48
 */
@Controller
@RequestMapping("/allocationCheck")
public class AllocationCheckController extends BaseController {

    //页面文件路径
    private String prefix = "allocation/allocationCheck";
    @Autowired
    private AllocationOrderService allocationOrderService;

    //加载页面
    @GetMapping("/page")
    @RequiresPermissions({"allocation:order:vew"})
    public String create(Model model) {

        // 获取登入用户实体
        User user = ShiroUtils.getUser();
        model.addAttribute("userName", user.getName());
        model.addAttribute("odrDate", new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
        return prefix + "/page";
    }

    /**
     * 调拨单审核页面数据
     * @param query
     * @return
     */
    @ResponseBody
    @PostMapping("/list")
    @RequiresPermissions({"allocation:order:vew"})
    public JsonResult list(AllocationOrderModel query){

        startPage();
        List<AllocationOrderModel> list = allocationOrderService.selectAllCheckByQuery(query);
        return jsonResult(list);
    }

    /***
     * 审核通过
     * @param odrId
     * @return
     */
    @ResponseBody
    @PostMapping("/commit")
    @RequiresPermissions({"allocation:order:vew"})
    public JsonResult commit(@Param("odrId") Integer odrId){

        return jsonResult(allocationOrderService.commitAllocationCheckByPrimaryKey(odrId));
    }

    /**
     * 审核驳回
     * @param odrId
     * @return
     */
    @ResponseBody
    @PostMapping("/reject")
    @RequiresPermissions({"allocation:order:vew"})
    public JsonResult reject(@Param("odrId") Integer odrId){

        return jsonResult(allocationOrderService.rejectAllocationCheckByPrimaryKey(odrId));
    }

    /**
     * 撤销审核通过
     * @param odrId
     * @return
     */
    @ResponseBody
    @PostMapping("/revoke")
    @RequiresPermissions({"allocation:order:vew"})
    public JsonResult revoke(@Param("odrId") Integer odrId){

        return jsonResult(allocationOrderService.revokeAllocationCheckByPrimaryKey(odrId));
    }

}
